/*
 * ResourceManager.h
 *
 *  Created on: May 11, 2012
 *      Author: mklingen
 */

#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H

#include "Resource.h"
#include <SFML/Graphics.hpp>
#include <map>

namespace kling
{

    // Class which handles abstract asset storage.
    class ResourceManager
    {
        public:
            ResourceManager();
            virtual ~ResourceManager();

            // Associates the given resource with the given name, and deletes the existing resource if it is there.
            void RegisterResource(std::string name, kling::Resource* resource);

            // Removes the given resource from the map and deletes it.
            void UnregisterResource(std::string name);

            // Unregisters every resource.
            void ClearResources();

            // Returns whether or not the resource with the given name is initialized.
            // if the resource does not exist, returns false.
            bool IsResourceInitialized(std::string name);

            // Initializes the resource with the given name. Returns false on failure.
            bool InitializeResource(std::string name);

            // Returns whether or not the given resource is registered.
            inline bool HasResource(std::string name) { return (m_resourceMap.find(name) != m_resourceMap.end()); }

            // Gets the resource of the given resource type. Returns false if the resource doesn't exist,
            // or if the resource is not of the given type.
            template <class ResourceType> ResourceType* GetResource(std::string name)
            {
                if(!HasResource(name))
                {
                    return NULL;
                }
                else
                {
                    kling::Resource* resource = m_resourceMap[name];
                    return dynamic_cast<ResourceType*>(resource);
                }
            }

        private:
            // Maps resource names to resources.
            std::map<std::string, kling::Resource*> m_resourceMap;

    };

} /* namespace arms */

#endif /* RESOURCEMANAGER_H*/
